﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GsOrganization.Common.Extend
{
    /// <summary>
    /// 字节流扩展方法
    /// </summary>
    public static class ByteExtend
    {
        /// <summary>
        /// 将字节流以UTF8格式转化为字符串
        /// </summary>
        /// <param name="bsData"></param>
        /// <returns></returns>
        public static string UTF8ToString(this byte[] bsData) => Encoding.UTF8.GetString(bsData);

        /// <summary>
        /// 将字节流以UTF8格式转化为字符串
        /// </summary>
        /// <param name="bsData"></param>
        /// <param name="start"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string UTF8ToString(this byte[] bsData, int start, int length) => Encoding.UTF8.GetString(bsData, start, length);

        /// <summary>
        /// 将字符串转化为UTF8格式的字节流
        /// </summary>
        /// <param name="SrcText"></param>
        /// <returns></returns>
        public static byte[] UTF8BytesFromString(this string SrcText) => Encoding.UTF8.GetBytes(SrcText);

        /// <summary>
        /// 将字节流以ASCII格式转化为字符串
        /// </summary>
        /// <param name="bsData"></param>
        /// <returns></returns>
        public static string AsciiToString(this byte[] bsData) => Encoding.ASCII.GetString(bsData);

        /// <summary>
        /// 将字节流以ASCII格式转化为字符串
        /// </summary>
        /// <param name="SrcText"></param>
        /// <returns></returns>
        public static byte[] AsciiBytesFromString(this string SrcText) => Encoding.ASCII.GetBytes(SrcText);

        /// <summary>
        /// 将字节流转换为十六进制字符串
        /// </summary>
        /// <param name="bsData"></param>
        /// <param name="isLower"></param>
        /// <returns></returns>
        public static string GetHexString(this byte[] bsData, bool isLower = true)
        {
            if (bsData == null)
                return null!;
            StringBuilder sb = new StringBuilder();
            if (isLower)
            {
                foreach (var b in bsData)
                {
                    sb.Append(b.ToString("x2"));
                }
            }
            else
            {
                foreach (var b in bsData)
                {
                    sb.Append(b.ToString("X2"));
                }
            }
            return sb.ToString();
        }

        /// <summary>
        /// 将字节流转换为十六进制字符串
        /// </summary>
        /// <param name="bsData"></param>
        /// <param name="StartIdx"></param>
        /// <param name="Length"></param>
        /// <param name="isLower"></param>
        /// <returns></returns>
        public static string GetHexString(this byte[] bsData, int StartIdx, int Length, bool isLower = true)
        {
            if (bsData == null)
                return null!;
            StringBuilder sb = new StringBuilder();
            if (isLower)
            {
                for (int i = 0; i < Length; i++)
                {
                    sb.Append(bsData[StartIdx + i].ToString("x2"));
                }
            }
            else
            {
                for (int i = 0; i < Length; i++)
                {
                    sb.Append(bsData[StartIdx + i].ToString("X2"));
                }
            }
            return sb.ToString();
        }

        /// <summary>
        /// 将字节流转换为Base64字符串
        /// </summary>
        /// <param name="bsData"></param>
        /// <returns></returns>
        public static string GetBase64String(this byte[] bsData)
        {
            if (bsData == null)
                return null!;
            return Convert.ToBase64String(bsData);
        }

        /// <summary>
        /// 将Base64字符串转换为字节流
        /// </summary>
        /// <param name="SrcText"></param>
        /// <returns></returns>
        public static byte[] FromBase64String(this string SrcText)
        {
            if (SrcText == null)
                return null!;
            try
            {
                return Convert.FromBase64String(SrcText);
            }
            catch
            {
                return null!;
            }
        }

        /// <summary>
        /// 将字节流转换为无符号短整型（ushort）
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] UShort2Bytes(this ushort data) => BitConverter.GetBytes(data);

        /// <summary>
        /// 将字节流转换为无符号短整型（ushort）
        /// </summary>
        /// <param name="bsData"></param>
        /// <param name="StartIndex"></param>
        /// <returns></returns>
        public static ushort Bytes2UShort(this byte[] bsData, int StartIndex = 0) => BitConverter.ToUInt16(bsData, StartIndex);

        /// <summary>
        /// 将无符号整型（uint）转换为字节流
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] UInt2Bytes(this uint data) => BitConverter.GetBytes(data);

        /// <summary>
        /// 将字节流转换为无符号整型（uint）
        /// </summary>
        /// <param name="bsData"></param>
        /// <param name="StartIndex"></param>
        /// <returns></returns>
        public static uint Bytes2UInt(this byte[] bsData, int StartIndex = 0) => BitConverter.ToUInt32(bsData, StartIndex);

        /// <summary>
        /// 将字节流转换为有符号整型（int）
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] Int2Bytes(this int data) => BitConverter.GetBytes(data);

        /// <summary>
        /// 将字节流转换为有符号整型（int）
        /// </summary>
        /// <param name="bsData"></param>
        /// <param name="StartIndex"></param>
        /// <returns></returns>
        public static int Bytes2Int(this byte[] bsData, int StartIndex = 0) => BitConverter.ToInt32(bsData, StartIndex);
    }
}
